﻿using System.ComponentModel;
using System.Windows.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using theeg.DependencyAnalysis.CommonAddin.Behaviors;

namespace UnitTests
{
    [TestClass]
    public class ViewSourceGroupTest
    {
        [TestInitialize]
        public void Initialize()
        {
            ViewSourceGroup.Clear();
        }

        [TestMethod]
        public void GroupsAreEmptyByDefault()
        {
            Assert.AreEqual(0, ViewSourceGroup.Groups.Count);
        }

        [TestMethod]
        public void AddToGroupCreatesNewGroup()
        {
            ViewSourceGroup.AddToGroup(new CollectionViewSource(), "test");
            Assert.AreEqual(1, ViewSourceGroup.Groups.Count);
        }

        [TestMethod]
        public void AddingTwoItemsToOneGroupCreatesOneGroup()
        {
            ViewSourceGroup.AddToGroup(new CollectionViewSource(), "test");
            ViewSourceGroup.AddToGroup(new CollectionViewSource(), "test");
            Assert.AreEqual(1, ViewSourceGroup.Groups.Count);
        }

        [TestMethod]
        public void AddingSameItemTwoTimesToOneGroupCreatesOneReference()
        {
            var collectionViewSource = new CollectionViewSource();
            ViewSourceGroup.AddToGroup(collectionViewSource, "test");
            ViewSourceGroup.AddToGroup(collectionViewSource, "test");
            Assert.AreEqual(1, ViewSourceGroup.Groups["test"].Count);
        }

        [TestMethod]
        public void AddingTwoItemsToTwoGroupsCreatesTwoGroups()
        {
            ViewSourceGroup.AddToGroup(new CollectionViewSource(), "test1");
            ViewSourceGroup.AddToGroup(new CollectionViewSource(), "test2");
            Assert.AreEqual(2, ViewSourceGroup.Groups.Count);
        }

        [TestMethod]
        public void SortUpdatesSortDescriptorOfCollectionViewSource()
        {
            var collectionViewSource = new CollectionViewSource();
            ViewSourceGroup.AddToGroup(collectionViewSource, "test");
            ViewSourceGroup.Sort("test", "Hello", ListSortDirection.Descending);
            var viewSource = ViewSourceGroup.Groups["test"][0];
            Assert.AreEqual(1, viewSource.SortDescriptions.Count);
            Assert.AreEqual("Hello", viewSource.SortDescriptions[0].PropertyName);
            Assert.AreEqual(ListSortDirection.Descending, viewSource.SortDescriptions[0].Direction);
        }

        [TestMethod]
        public void ToggleSortationChangesSortDirection()
        {
            var collectionViewSource = new CollectionViewSource();
            ViewSourceGroup.AddToGroup(collectionViewSource, "test");
            ViewSourceGroup.Sort("test", "Hello", ListSortDirection.Descending);
            ViewSourceGroup.ToggleSortation("test", "Hello");
            var viewSource = ViewSourceGroup.Groups["test"][0];
            Assert.AreEqual(1, viewSource.SortDescriptions.Count);
            Assert.AreEqual("Hello", viewSource.SortDescriptions[0].PropertyName);
            Assert.AreEqual(ListSortDirection.Ascending, viewSource.SortDescriptions[0].Direction);
        }
    }
}
